package com.laoxu.java.bootlayuicurd.controller;

import com.laoxu.java.bootlayuicurd.entity.LayuiUser;
import com.laoxu.java.bootlayuicurd.entity.ResultBean;
import com.laoxu.java.bootlayuicurd.service.LayuiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:  用户controller
 * @Author laoxu
 * @Date 2019/12/21 10:11
 **/
@RestController
@RequestMapping("/api/layui/user")
public class LayuiUserController {
    @Autowired
    LayuiUserService userService;

    @GetMapping("/list")
    public ResultBean list(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit){
        List<LayuiUser> users = userService.getPager(page,limit);
        int count = userService.getAllUsers().size();

        ResultBean result = new ResultBean(0,"查询成功",count,users);

        return result;
    }

    @PostMapping("/save")
    public ResultBean save(@RequestBody LayuiUser user){
        // 判断是新增还是修改
        if(user.getId()==0){
            userService.addUser(user);
        }else{
            userService.updateUser(user);
        }

        return new ResultBean(200,"保存成功",0,"");
    }

    @PostMapping("/remove")
    public ResultBean modify(@RequestBody int[] ids){
        userService.delUsers(ids);
        return new ResultBean(200,"删除成功",0,"");
    }
}
